<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;

class PickupPointApiController extends Controller
{
    /**
     * 提交自提点申请
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function apply(Request $request): JsonResponse
    {
        try {
            // 验证请求数据
            $validated = $request->validate([
                'name' => 'required|string|max:100',
                'phone' => 'required|string|max:20',
                'address' => 'required|string|max:200',
                'longitude' => 'required|numeric',
                'latitude' => 'required|numeric',
                'category' => 'required|string|in:restaurant,retail,service,other',
                'business_hours' => 'required|string|max:100',
                'description' => 'nullable|string|max:500'
            ]);

            // 模拟生成申请ID
            $applicationId = 'PP' . date('Ymd') . rand(1000, 9999);

            return response()->json([
                'code' => 200,
                'message' => '申请提交成功',
                'data' => [
                    'application_id' => $applicationId,
                    'status' => 'pending',
                    'message' => '您的申请已提交，我们将在3个工作日内审核'
                ]
            ]);

        } catch (\Illuminate\Validation\ValidationException $e) {
            return response()->json([
                'code' => 422,
                'message' => '数据验证失败',
                'data' => $e->errors()
            ], 422);
        } catch (\Exception $e) {
            return response()->json([
                'code' => 500,
                'message' => '服务器错误',
                'data' => null
            ], 500);
        }
    }

    /**
     * 获取自提点列表
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function index(Request $request): JsonResponse
    {
        try {
            $page = $request->get('page', 1);
            $limit = $request->get('limit', 10);
            $district = $request->get('district');
            $status = $request->get('status', 'normal');

            // 模拟自提点数据
            $pickupPoints = [
                [
                    'id' => 48,
                    'name' => '沙县小吃',
                    'district' => '武汉市硚口区',
                    'address' => '建设大道123号',
                    'phone' => '13811111234',
                    'status' => '正常',
                    'longitude' => 114.305,
                    'latitude' => 30.593,
                    'business_hours' => '08:00-22:00',
                    'distance' => 0.5,
                    'rating' => 4.5,
                    'category' => '餐饮'
                ],
                [
                    'id' => 47,
                    'name' => '赣州之正装饰设计有限公司',
                    'district' => '武汉市硚口区',
                    'address' => '解放大道456号',
                    'phone' => '13811111235',
                    'status' => '正常',
                    'longitude' => 114.308,
                    'latitude' => 30.595,
                    'business_hours' => '09:00-18:00',
                    'distance' => 0.8,
                    'rating' => 4.2,
                    'category' => '服务'
                ],
                [
                    'id' => 46,
                    'name' => '拾贝服饰',
                    'district' => '武汉市硚口区',
                    'address' => '中山大道789号',
                    'phone' => '13811111236',
                    'status' => '正常',
                    'longitude' => 114.310,
                    'latitude' => 30.590,
                    'business_hours' => '10:00-21:00',
                    'distance' => 1.2,
                    'rating' => 4.3,
                    'category' => '零售'
                ]
            ];

            // 分页处理
            $total = count($pickupPoints);
            $offset = ($page - 1) * $limit;
            $data = array_slice($pickupPoints, $offset, $limit);

            return response()->json([
                'code' => 200,
                'message' => '获取成功',
                'data' => [
                    'list' => $data,
                    'pagination' => [
                        'current_page' => (int)$page,
                        'per_page' => (int)$limit,
                        'total' => $total,
                        'last_page' => ceil($total / $limit)
                    ]
                ]
            ]);

        } catch (\Exception $e) {
            return response()->json([
                'code' => 500,
                'message' => '服务器错误',
                'data' => null
            ], 500);
        }
    }

    /**
     * 获取自提点详情
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function show(Request $request): JsonResponse
    {
        try {
            // 从请求体中获取自提点ID
            $id = $request->input('id');
            
            if (!$id) {
                return response()->json([
                    'code' => 400,
                    'message' => '缺少自提点ID参数',
                    'data' => null
                ], 400);
            }

            // 模拟自提点详情数据
            $pickupPoint = [
                'id' => $id,
                'name' => '沙县小吃',
                'category' => 'restaurant',
                'business_status' => 'normal',
                'province' => '湖北省',
                'city' => '武汉市', 
                'district' => '硚口区',
                'address' => '建设大道123号',
                'longitude' => 114.305,
                'latitude' => 30.593,
                'phone' => '13811111234',
                'business_hours' => '08:00-22:00',
                'establishment_date' => '2025-08-05',
                'manager' => [
                    'name' => '张经理',
                    'phone' => '13811111234'
                ],
                'status' => '正常',
                'created_at' => '2024-01-15 10:30:00',
                'rating' => 4.5,
                'review_count' => 128
            ];

            return response()->json([
                'code' => 200,
                'message' => '获取成功',
                'data' => $pickupPoint
            ]);

        } catch (\Exception $e) {
            return response()->json([
                'code' => 500,
                'message' => '服务器错误',
                'data' => null
            ], 500);
        }
    }

    /**
     * 搜索附近的自提点
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function nearby(Request $request): JsonResponse
    {
        try {
            $latitude = $request->get('latitude');
            $longitude = $request->get('longitude');
            $radius = $request->get('radius', 5); // 默认5公里范围内

            if (!$latitude || !$longitude) {
                return response()->json([
                    'code' => 400,
                    'message' => '缺少位置参数',
                    'data' => null
                ], 400);
            }

            // 模拟附近的自提点数据
            $nearbyPoints = [
                [
                    'id' => 48,
                    'name' => '沙县小吃',
                    'distance' => 0.5,
                    'address' => '建设大道123号',
                    'phone' => '13811111234',
                    'category' => '餐饮'
                ],
                [
                    'id' => 47,
                    'name' => '赣州之正装饰设计有限公司',
                    'distance' => 0.8,
                    'address' => '解放大道456号',
                    'phone' => '13811111235',
                    'category' => '服务'
                ]
            ];

            return response()->json([
                'code' => 200,
                'message' => '获取成功',
                'data' => $nearbyPoints
            ]);

        } catch (\Exception $e) {
            return response()->json([
                'code' => 500,
                'message' => '服务器错误',
                'data' => null
            ], 500);
        }
    }
}

